

import tensorflow as tf
import csv

#读取csv数据
filename_queue = tf.train.string_input_producer(["./data_1507814711443.csv", "./data_1507832409960.csv",
	"./data_1507849915485.csv", "./data_1507867537281.csv"])

reader = tf.TextLineReader()
key, value = reader.read(filename_queue)

# Default values, in case of empty columns. Also specifies the type of the
# decoded result.
record_defaults = [[0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0]]

col1, col2, col3, col4, col5, col6, col7, col8, col9, col10, col11, col12, col13, col14, col15, col16, col17, col18, col19, col20, col21, col22 = tf.decode_csv(value, record_defaults=record_defaults)

key = col1
features = [col3, col4, col5, col6, col7, col8, col9, col10, col11, col12, col13, col14, col15, col16, col17, col18, col19, col20]
judgeLabel = [col21, col22]

learning_rate = 0.001

n_hidden_1 = 18
n_hidden_2 = 18
n_hidden_3 = 18
n_input = 18
n_classes = 2

x = tf.placeholder(tf.float32, [None, n_input])
y_ = tf.placeholder(tf.float32, [None, n_classes])

def multilayer_perceptron(x, weights, biases):
	layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1'])
	layer_1 = tf.nn.relu(layer_1)

	layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2'])
	layer_2 = tf.nn.relu(layer_2)

	layer_3 = tf.add(tf.matmul(layer_2, weights['h3']), biases['b3'])
	layer_3 = tf.nn.relu(layer_3)

	out_layer = tf.matmul(layer_3, weights['out']) + biases['out']
	return out_layer


weights = {
	'h1': tf.Variable(tf.random_normal([n_input, n_hidden_1])),
	'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])),
	'h3': tf.Variable(tf.random_normal([n_hidden_2, n_hidden_3])),
	'out': tf.Variable(tf.random_normal([n_hidden_3, n_classes]))
}

biases = {
	'b1': tf.Variable(tf.random_normal([n_hidden_1])),
	'b2': tf.Variable(tf.random_normal([n_hidden_2])),
	'b3': tf.Variable(tf.random_normal([n_hidden_3])),
	'out': tf.Variable(tf.random_normal([n_classes]))
}

y = multilayer_perceptron(x, weights, biases)


#cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
cross_entropy = tf.reduce_mean(
      tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))

train_step = tf.train.AdamOptimizer(learning_rate).minimize(cross_entropy)
#train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
totalAccuracy = 0

saver = tf.train.Saver()

# Train
with tf.Session() as sess:
	sess.run(tf.global_variables_initializer())
	coord = tf.train.Coordinator()
	threads = tf.train.start_queue_runners(coord=coord)

	batch_xs, batch_ys = [], []
	for i in range(300000):
		example, label = sess.run([features, judgeLabel])
		batch_xs.append(example)
		batch_ys.append(label)

		if i % 100 == 0:
			sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
#		
			correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
			accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
			train_accuracy = accuracy.eval(feed_dict={x: batch_xs, y_: batch_ys})
			print('step %d, training accuracy %g' % (i, train_accuracy))
			batch_xs, batch_ys = [], []
	
	saver.save(sess, "Model/haixue_new_3_model.ckpt")
	print("---------------test--------------")
	with open("test_new3.csv","w+") as csvfile:
		csv_writer = csv.writer(csvfile)

		chanceId_set = []
		data_set = []

		with open('haixue_pre_handle_data_1506505603298.csv', 'r') as f:
			reader = csv.reader(f)
			for row in reader:
				chanceId = row[0]
				example = [int(row[2]), int(row[3]), int(row[4]), int(row[5]), int(row[6]), int(row[7]), int(row[8]), int(row[9]), int(row[10]), 
				int(row[11]), int(row[12]), int(row[13]), int(row[14]), int(row[15]), int(row[16]), int(row[17]), int(row[18]), int(row[19])]
				
				chanceId_set.append(chanceId)
				data_set.append(example)
			
		result = tf.argmax(y, 1)
		predictResult = result.eval(feed_dict={x: data_set})
		
		for i in range(88070): 
			list = [chanceId_set[i], predictResult[i]]
			print("index: %s, list: %s" % (i, list) )
			csv_writer.writerow(list)





